<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>登陆日志-主页</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索条件(<b style="color: #1E9FFF;cursor: pointer">收起--展开</b>)</legend>
            <form class="layui-form layui-form-pane" lay-filter="search-form" id="search-form" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="userName" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">会话编码</label>
                        <div class="layui-input-inline" style="width: 300px;">
                            <input type="text" name="sessionId" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">登陆地址</label>
                        <div class="layui-input-inline">
                            <input type="text" name="loginIp" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">登陆状态</label>
                        <div class="layui-input-inline">
                            <select name="loginStatus">
                                <option value="">请选择</option>
                                <option value="0">成功</option>
                                <option value="1">失败</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <input id="startDate" name="startDate" type="hidden" value=""/>
                        <input id="endDate" name="endDate" type="hidden" value=""/>
                        <label class="layui-form-label">登陆时间</label>
                        <div class="layui-input-inline" style="width: 180px;">
                            <input type="text" autocomplete="off" id="createTimeRange" class="layui-input" placeholder="开始日期~结束时间">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn layui-bg-blue"  lay-submit lay-filter="search-btn"><i class="layui-icon layui-icon-search"></i> 搜 索</button>
                        <button type="reset" class="layui-btn layui-btn-primary"  lay-submit lay-filter="reset-btn"><i class="layui-icon layui-icon-refresh"></i> 重 置</button>
                    </div>
                </div>
            </form>
        </fieldset>
        <!-- 表格头部左侧工具栏 -->
        <script type="text/html" id="toolbar">
            <div class="layui-btn-group layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>
                <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="clear"><i class="layui-icon layui-icon-clear"></i>清空</button>
            </div>
        </script>
        <!-- 数据表格 -->
        <table class="layui-hide" id="loginLogList" lay-filter="loginLogListFilter"></table>
    </div>
</div>
<script th:src="@{/layuimini/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script th:src="@{/layuimini/js/lay-config.js?v=1.0.4}" charset="utf-8"></script>
<script th:src="@{/layuimini/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/js/table-form.js}" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript">
    layui.use(['form', 'laydate', 'table','notice'], function () {
        let $ = layui.jquery,
            form = layui.form,
            laydate = layui.laydate,
            notice = layui.notice,
            table = layui.table;

        let ctxPath = /*[[@{/}]]*/'';

        //渲染时间
        laydate.render({
            elem: '#createTimeRange',
            type: 'date',
            range: '~',
            format: 'yyyy/MM/dd',
            done: function(value, date1, date2){
                $('#startDate').val(startDateTimeFormat(date1));
                $('#endDate').val(endDateTimeFormat(date2));
            }
        });

        //渲染表格
        let tableIns = table.render({
            elem: '#loginLogList',
            url: ctxPath+'system/log/LoginLoglist',
            title:'登陆日志信息',
            height: 'full-100',
            method : 'GET',
            toolbar: '#toolbar',                                //绑定表格头部左侧工具栏
            defaultToolbar: ['filter', 'exports', 'print', {    //开启默认表格头部右侧图标
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {type: 'checkbox', width: 80,align:'center'},
                    {field: 'recordId', width: 150, title: '日志编号',align:'center',sort:true,hide:true},
                    {field: 'sessionId', width: 310, title: '会话ID',align:'center',sort:true},
                    {field: 'userName', width: 90, title: '用户名',align:'center',sort:true},
                    {field: 'nickName', width: 100, title: '真实姓名',align:'center'},
                    {field: 'loginIp', width: 120, title: '登陆IP',align:'center'},
                    {field: 'location', width: 180, title: '登陆地址',align:'center',hide:true},
                    {field: 'browser', width: 100, title: '浏览器',align:'center'},
                    {field: 'os', width: 110, title: '操作系统',align:'center'},
                    {field: 'loginStatus', width: 80, title: '状态',align:'center',sort:true, templet:function(d){
                        return (0 === d.loginStatus) ? '成功' : '失败'
                    }},
                    {field: 'loginDesc', width: 160, title: '日志信息',align:'center'},
                    {field: 'loginTime', width: 160, title: '登陆时间',align:'center',sort:true},
                    {field: 'logoutTime', width: 160, title: '退出时间',align:'center'},
                    {field: 'loginDuration', width: 80, title: '登陆时长(时)',align:'center', templet:function(d){
                        return (0.0 === d.loginDuration) ? '' : d.loginDuration+'h'
                    }}
                ]
            ],
            page: true,
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15
        });

        //监听查询条件提交按钮
        form.on('submit(search-btn)', function (data) {
            //执行搜索重载
            table.reload('loginLogList', {
                page: {curr: 1},
                where: form.val('search-form')
            }, 'data');
            return false;
        });

        //监听表格头部工具栏事件
        table.on('toolbar(loginLogListFilter)', function (obj) {
            switch (obj.event) {
                case 'delete':
                    let checkStatus = table.checkStatus(obj.config.id);
                    let recordIds = '';
                    $.each(checkStatus.data,function (k,v){
                        recordIds = recordIds + ','+v.recordId;
                    })
                    dealOperLog(recordIds);
                    break;
                case 'clear':
                    dealOperLog('All');
                    break;
            };
        });

        //批量删除日志
        function dealOperLog(datas) {
            let delTips = '您确定要删除选中的记录吗';
            if(datas === 'All'){
                datas = '',
                delTips = '您确定要清空所有的登陆日志吗';
            }else if(datas.length === 0){
                notice.warning('请选择需要删除的记录！');
                return;
            }

            layer.confirm(delTips,{title:'提示',icon:0,btn:['确定','取消']},function (index) {
                layer.close(index);
                $.ajax({
                    type:'PUT',
                    url:ctxPath+'system/log/deleteLoginLog',
                    data:{recordIds:datas},
                    dataType:'json',
                    success:function (res){
                        if(res.code === 200){
                            notice.success('删除成功',function(){
                                tableIns.reload();
                            });
                        }else{
                            notice.error(res.msg);
                        }
                    }
                });
            })
        }
    });
</script>
</body>
</html>